package com.example.apptest.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.example.app_test.R;

import androidx.viewpager2.widget.ViewPager2;

public class BarView extends RelativeLayout implements View.OnClickListener {

    private ViewPager2 viewPager;
    private ButtonView2 btnAnim;
    private ButtonView2 btnList;
    private ButtonView2 btnBle;

    @Override
    public void onClick(View view) {
        btnAnim.setChecked(false);
        btnList.setChecked(false);
        btnBle.setChecked(false);
        if(view.getId() == R.id.btn_anim) {
            viewPager.setCurrentItem(0);
            btnAnim.setChecked(true);
        } else if(view.getId() == R.id.btn_list) {
            viewPager.setCurrentItem(1);
            btnList.setChecked(true);
        } else if(view.getId() == R.id.btn_ble) {
            viewPager.setCurrentItem(2);
            btnBle.setChecked(true);
        }
    }



    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.bar, this);
        btnAnim = findViewById(R.id.btn_anim);
        btnList = findViewById(R.id.btn_list);
        btnBle = findViewById(R.id.btn_ble);

        btnAnim.setActivateIco(R.drawable.run_green);
        btnAnim.setDeactivateIco(R.drawable.run);
        btnList.setActivateIco(R.drawable.list_gray_green);
        btnList.setDeactivateIco(R.drawable.list_gray);
        btnBle.setActivateIco(R.drawable.bluetooth_blue);
        btnBle.setDeactivateIco(R.drawable.bluetooth_gray);

        btnAnim.setBtnName("动画");
        btnList.setBtnName("列表");
        btnBle.setBtnName("蓝牙");

        btnAnim.setChecked(true);
        btnList.setChecked(false);
        btnBle.setChecked(false);

        btnAnim.setOnClickListener(this);
        btnList.setOnClickListener(this);
        btnBle.setOnClickListener(this);
    }

    public BarView(Context context) {
        super(context);
        init();
    }

    public BarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private int bleMsgCount = 10;
    private int old_pos = 0;
    public void setViewPager(ViewPager2 vp) {
        viewPager = vp;
        viewPager.registerOnPageChangeCallback(
                new ViewPager2.OnPageChangeCallback() {
                    @Override
                    public void onPageSelected(int pos) {
                        if(pos > old_pos)
                            btnBle.setMsgCount(++bleMsgCount);
                        else
                            btnBle.setMsgCount(--bleMsgCount);
                        old_pos = pos;
                        btnAnim.setChecked(pos == 0);
                        btnList.setChecked(pos == 1);
                        btnBle.setChecked(pos == 2);
                    }
                }
        );

    }
}
